Skip to content

Shell Programming

  • Shell: Command-line interface between user and OS that interprets and executes commands.

  • Common shells: sh (Bourne shell), bash (Bourne Again Shell, default in Linux), zsh (Z shell).

  • Script file extension: .sh

  • Execution:

    Terminal window
    bash file.sh # run using bash
    chmod +x file.sh # make executable
    ./file.sh # execute script directly
  • Shebang (#!): Specifies interpreter for the script, e.g., #!/bin/bash.


  • User-defined variables: No spaces around =

    Terminal window
    x=10
    name="SEBI"
    echo $x

    $ expands variable value.

  • Environment variables:

    • HOME: Current user’s home directory.

    • PATH: Colon-separated directories where shell searches commands.

  • Exporting variables:

    Terminal window
    export x

    Makes variable available to child processes/subshells.


  • Read input:

    Terminal window
    read x
  • Prompted input:

    Terminal window
    read -p "Enter value: " x
  • Print output:

    Terminal window
    echo "Value is $x"

  • Store command output:

    Terminal window
    d=$(date) # modern syntax
    files=`ls` # legacy syntax
  • Arithmetic operations:

    Terminal window
    sum=$((a+b))
    mul=`expr $a \* $b`

  • if syntax:

    Terminal window
    if [ $a -gt $b ]; then
    echo "a greater"
    fi
  • Comparison operators (integers): -eq, -ne, -gt, -lt, -ge, -le

  • String operators: =, !=, -z (empty), -n (non-empty)

  • if-else syntax:

    Terminal window
    if [ $a -eq 10 ]; then
    echo "Ten"
    else
    echo "Not ten"
    fi

Terminal window
case $x in
1) echo "One" ;;
2) echo "Two" ;;
*) echo "Other" ;;
esac
  • case: Starts multi-way selection.

  • in: Begins pattern list.

  • 1): Pattern match.

  • *): Default case.

  • ;;: Ends a block.

  • esac: Ends case statement.


  • For loop:

    Terminal window
    for i in 1 2 3; do
    echo $i
    done
  • While loop:

    Terminal window
    while [ $x -lt 5 ]; do
    x=$((x+1))
    done
  • Until loop:

    Terminal window
    until [ $x -gt 5 ]; do
    x=$((x+1))
    done

Terminal window
fun() {
echo "Hello"
}
fun
  • Return status via $?.

Terminal window
$0 # script name
$1 $2 ... # arguments
$# # number of arguments
$@ # all arguments

  • -f: file exists

  • -d: directory exists

  • -r: readable, -w: writable, -x: executable

Example:

Terminal window
if [ -f file.txt ]; then echo "Exists"; fi

  • > : overwrite output

  • >>: append output

  • < : input from file

  • 2>: redirect errors

Terminal window
ls > out.txt

  • Send output of one command to another:
Terminal window
ps -ef | grep root
  • ps: list processes, -ef: full format

  • grep: search text/pattern, root: matches processes owned by root


  • File operations: ls cd pwd mkdir rmdir rm cp mv

  • View files: cat less more head tail

  • Text processing: grep awk sed sort uniq wc cut

  • Permissions: chmod chown

  • Process management: ps top kill

  • Disk & memory: df du free


  • awk: extract/format text columns

    Terminal window
    awk '{print $1}' file.txt
  • sed: find/replace/modify text

    Terminal window
    sed 's/old/new/' file.txt

  • 0 = success, non-zero = failure
Terminal window
echo $?

  • Schedule jobs:
Terminal window
crontab -e
  • -e: edit user’s cron jobs

  • Format:

min hour day month week command

  • Focus on practical usage of shell commands, loops, conditions, and file operations.

  • Remember command vs arithmetic substitution: $() vs $(( )).

  • Know pipes, redirection, cron jobs, awk & sed basics.

  • Expect questions on environment variables, file tests, and exit status.


If you want, I can create a super-condensed one-page “last-minute revision sheet” for shell scripting for SEBI IT Grade A. It will fit everything exam-relevant in bare minimum lines. Do you want me to do that?